// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

var ClickManager = require('ClickManager')
var CreatorHelper = require('CreatorHelper')
var NetworkManager = require('NetworkManager')

var headimgList = cc.Class({
    name: 'headimgList',
    properties: {
        id: 0,
        borderSprite: cc.SpriteFrame,
        headimg: cc.SpriteFrame
    }
});

cc.Class({
    extends: cc.Component,
    properties: {
        items: {
            default: [],
            type: headimgList
        },

        itemPrefab: cc.Prefab,
        changeHeadImgBtn: cc.Node, // 更换头像按钮
        alertPrefab: cc.Prefab,

        selectHeadImgId: 0,
        token: null,
    },

    onLoad () {
        var borderPath = 'images/smallimages/hanl/header/'
        var itemIndex  = []

        this.token = cc.sys.localStorage.getItem('__TOKEN__')

        for (var i = 0; i < this.items.length; ++i) {
            let item = cc.instantiate(this.itemPrefab)
            var data = this.items[i]
            item.idx = i

            this.node.addChild(item)
            item.getComponent('HeadimgTemplate').init(data)

            ClickManager.onClick(item, item, (args) => {
                for (var i = 0; i < this.items.length; i++) {
                    this.items[i].borderSprite = new cc.SpriteFrame(borderPath + 'head_border')
                    this.items[i].y = -2
                    data = this.items[i]
                    this.node.children[i].getComponent('HeadimgTemplate').init(data)
                }

                this.selectHeadImgId = item.idx
                this.items[item.idx].borderSprite = new cc.SpriteFrame(borderPath + 'head_border_ck')
                this.items[item.idx].y = -2
                data = this.items[item.idx]
                item.getComponent('HeadimgTemplate').init(data)
            })
        }

        // 更换头像
        ClickManager.onClick(this.changeHeadImgBtn, this, () => {
            this.doChangeHeadImg()
        })
    },

    // 更换头像
    doChangeHeadImg () {
        var scene = cc.director.getScene()

        try {
            NetworkManager.onConnectedToHall( (hallService) => {
                hallService.proxy.changeHeadimg(this.token, this.selectHeadImgId + 1, (data) => {
                    let alert = cc.instantiate(this.alertPrefab)
                    alert.parent = scene

                    if (data.ok && data.code === 200) {
                        NetworkManager.updateUserInfo() // 更新用户信息
                        alert.getComponent('Alert').run('头像更换成功')
                    } else {
                        alert.getComponent('Alert').run(data.msg)
                    }
                })
            })
        } catch (err) {
            console.log('异常处理，待处理:', err)
            cc.director.loadScene('login')
        }
    }
});
